import { getMessage } from "../api/index.js"
import delay from "./delay.js";

export const gptReply = async (content, room, contact,type='room') => {

    const pushRoom = (askObj,ansObj,res)=>{
        console.log(res)
      let list =  room.messageList || []
      
        if(list.length>=10){
           list.shift()
           list.shift()
        }
      
        list.push(askObj)
        list.push(ansObj)
        console.log(list)
         room.messageList=list
    }
    const messageList = JSON.parse(JSON.stringify(room.messageList|| [])) 
    // console.log(cache)
    let askObj = {"role": "user","content":content}
    messageList.push(askObj)
    console.log( /mochen|葫芦爷救娃娃|/.test(contact.payload.name))
    const params = /mochen|葫芦爷救娃娃/.test(contact.payload.name) ? 
    { msg: messageList,token:300 } : { msg: messageList }
    // if(/二狗/.test(contact.payload.name)){
    //     params.token = 10
    // }
    const res = await getMessage(params)
    try {
        await delay(100);
        let says = res.data.result

        if (room && contact && type=='room') {

            if (/二狗/.test(contact.payload.name)) {

                return room.say(`二狗${says}`);
            }
            room.say`${contact} ${says}`;
            let ansObj = {"role": "assistant","content":says}
            pushRoom(askObj,ansObj,res)
            return says
        } else {
            let ansObj = {"role": "assistant","content":says}
            pushRoom(askObj,ansObj,res)

            return says
        }

    } catch (e) {
        room.say`你说的我不理解,要不你再说一次？`
        console.log(e)
        return e
    }
}




  